//
//  MediaManager.m
//  DaYing
//
//  Created by DaYing  on 2023.
//  Copyright © 2023 DaYing . All rights reserved.
//

#import "MediaManager.h"

@implementation MediaManager

+ (instancetype)shareInstance {
    static MediaManager *_manager = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _manager = [[self alloc] init];
    });
    return _manager;
}

//请求相册权限
- (void)requestPhotoAuthorizationStatus:(void (^)(BOOL granted))completion {
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusAuthorized) {//已经授权

        dispatch_async(dispatch_get_main_queue(), ^{
            
            UserManager *userManager = kUserManager.userManager;
            userManager.isMedia = YES;
            [kUserManager saveUserData:userManager];
            
            if (completion) {
                completion(YES);
            }
        });
        
    } else {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {

                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    UserManager *userManager = kUserManager.userManager;
                    userManager.isMedia = YES;
                    [kUserManager saveUserData:userManager];
                    
                    if (completion) {
                        completion(YES);
                    }
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (completion) {
                        completion(NO);
                    }
                });
            }
        }];
    }
}

//打开相机权限
- (void)requestCameraAuthorizationStatus:(void (^)(BOOL granted))completion {
    
    //判断是否有相机权限
    
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (granted) {
                if (completion) {
                    completion(YES);
                }
            } else {
                if (completion) {
                    completion(NO);
                }
                [self showAlertViewAboutNotAuthorAccessCamera];
            }
        });
    }];
}

//请求麦克风权限
- (void)requestMaikeAuthorizationStatus:(void (^)(BOOL granted))completion {
    
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    //申请日历权限
    if (status == AVAuthorizationStatusAuthorized) {//已经授权
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(YES);
            }
        });
    } else {
        
        [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
            if (granted) {
                if (completion) {
                    completion(YES);
                }
            } else {
                if (completion) {
                    completion(NO);
                }
            }
        }];
    }
}

#pragma mark - other
//提示用户进行麦克风使用授权
- (void)showSetAlertView {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要你同意使用麦克风，开启权限后才能使用拍照搜题功能" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *setAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //跳入当前App设置界面
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:setAction];
    
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    [window.rootViewController presentViewController:alertVC animated:YES completion:nil];
}

//提示用户进行蓝牙使用授权
- (void)showAlertViewAboutNotAuthorAccessBluetooth {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要你同意使用蓝牙权限，开启权限后才能使用遥控器功能" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *setAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //跳入当前App设置界面
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:setAction];
    
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    [window.rootViewController presentViewController:alertVC animated:YES completion:nil];
}

//提示用户进行蓝牙使用授权
- (void)showAlertViewAboutNotOpenBluetooth {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要你开启蓝牙，开启后并且授权应用使用蓝牙才能使用遥控器功能" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:cancelAction];
    
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    [window.rootViewController presentViewController:alertVC animated:YES completion:nil];
}

//提示没有相册权限
- (void)showAlertViewAboutNotAuthorAccessPhoto {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要你同意使用相册权限才能为你保存生成的二维码图片" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
                
            }];
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [kAppDelegate.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}

//提示没有相机权限
- (void)showAlertViewAboutNotAuthorAccessCamera {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要你同意使用相机权限，才能拍照使用搜题功能" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
                    
                }];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [kAppDelegate.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    });
}

@end
